/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import entity.Pessoa;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.swing.JOptionPane;

/**
 *
 * @author thiago_cyrino
 */
public class EntityDAO<E> {
    
    EntityManagerFactory factory;
    EntityManager en;
    EntityTransaction transaction;

    public EntityDAO() {
        factory = Persistence.createEntityManagerFactory("ProjetoJPAPU");
        en = factory.createEntityManager();
        transaction = en.getTransaction();
    }

    public boolean salva(E obj) {
        try {
            transaction.begin();
            en.persist(obj);
            transaction.commit();
        } catch (Exception e) {
            transaction.rollback();
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
        return false;
    }

    public boolean alterar(E obj) {
        try {
            transaction.begin();
            en.merge(obj);
            transaction.commit();
        } catch (Exception e) {
            transaction.rollback();
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
        return false;

    }

    public boolean deletar( E obj ) {
        try {
            transaction.begin();
            Object o = en.merge(obj);
            en.remove( o );
            transaction.commit();
        } catch (Exception e) {
            transaction.rollback();
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
        return false;
    }

}
